const app = (() => {
    const {
        StringStyleUtil,
        StringStyleType: {
            Camel,
            Snake,
        },
    } = rx
    let initDealData = {
        /**
         * 处理备注
         */
        msg: '',
        /**
         * 1、处理 2、取消处理
         */
        action: 1,
    }
    const filingWindowMap = new Map()
        .set('regDomainCount', {
            title: '已备案网站列表',
            filingStatus: 1,
        })
        .set('unRegDomainCount', {
            title: '未备案网站列表',
            filingStatus: 0,
        })
    return new BasicTableVue({
        data() {
            return {
                api: slogHotIpApi,
                columns: [
                    new TableColumn({width: 30, type: 'selection'}),
                    new TableColumn({prop: 'houseName', title: '所在机房'}),
                    new TableColumn({prop: 'findIpListIp', title: '应用IP'}),
                    new TableColumn({prop: 'visitsCount', title: '访问量'}),
                    new TableColumn({prop: 'siteCount', title: '网站数量'}),
                    new TableColumn({prop: 'regDomainCount', title: '已备案网站', customComponent: true}),
                    new TableColumn({prop: 'unRegDomainCount', title: '未备案网站', customComponent: true}),
                    new TableColumn({
                        prop: 'isInIpSeg',
                        title: '报备状态',
                        formatter: fieldFormatter.facade(fieldFormatter.hotIp.reportStatusMap),
                    }),
                    new TableColumn({
                        prop: 'block',
                        title: '阻断状态',
                        formatter: fieldFormatter.facade(fieldFormatter.hotIp.blockMap),
                    }),
                    new TableColumn({
                        prop: 'dealStatus',
                        title: '处置状态',
                        formatter: fieldFormatter.facade(fieldFormatter.hotIp.dealStatusMap),
                    }),
                    new TableColumn({
                        prop: 'protocol',
                        title: '协议',
                        formatter: fieldFormatter.facade(fieldFormatter.hotIp.protocolMap),
                    }),
                    new TableColumn({
                        prop: 'port',
                        title: '端口',
                    }),
                    new TableColumn({
                        prop: 'findIpListFirstTime',
                        title: '首次采集时间',
                        formatter: TableColumn.datetimeFormat,
                    }),
                    new TableColumn({
                        prop: 'findIpListLastTime',
                        title: '最后活跃时间',
                        formatter: TableColumn.datetimeFormat,
                    }),
                ],
                filingWindow: {
                    title: '',
                    show: false,
                    columns: [
                        new TableColumn({prop: 'ip', title: '应用IP'}),
                        new TableColumn({prop: 'domain', title: '域名'}),
                        new TableColumn({prop: 'visitsCount', title: '访问量'}),
                        new TableColumn({prop: 'idcId', title: '备案号/解析IP'}),
                        new TableColumn({
                            prop: 'topDomainflag',
                            title: '类型',
                            formatter: fieldFormatter.facade(fieldFormatter.slogHotDomain.topDomainflagMap),
                        }),
                        new TableColumn({
                            prop: 'findDomainListFirstTime',
                            title: '首次登记时间',
                            formatter: TableColumn.datetimeFormat,
                        }),
                        new TableColumn({
                            prop: 'findDomainListLastTime',
                            title: '最后活跃时间',
                            formatter: TableColumn.datetimeFormat,
                        }),
                    ],
                    data: [],
                },
                slogHotIpMap: new Map(),
                /*上报信息*/
                msgInfo: '',
                /*上报数量*/
                reportNum: '',
                /*上报框是否显示*/
                isReportShow: false,
                /**
                 * 批量处理显示
                 */
                isTabHandleShow: false,
                dealData: initDealData,
                /**
                 * 处理结果 82屏蔽 81放行
                 */
                dealResultStatus: 82,
                baseHouseInfoList: [],
            }
        },
        methods: {
            createForm() {
                return new HotIp({
                    firstTimeRange: [],
                    lastTimeRange: [],
                    findIpListIp: forward.getData().content,
                })
            },
            async init() {
                await this.initHouseInfo()
            },
            async getPage(page, entity) {
                const result = await this.api.page(page, rx.excludeFields(rx.blankToNullField(this.form), ...Array.from(this.excludeFieldSet)))
                this.slogHotIpMap = rx.arrayToMap(result.records, ({id}) => id)
                return result
            },
            initHouseInfo: async function () {
                this.baseHouseInfoList = await baseHouseInfoApi.selectList();
            },
            async reportShow() {
                this.reportNum = this.selectedIdList.length
                if (this.reportNum <= 0) {
                    await rxPrompt.warningMsg('没有符合规则活跃ip')
                    return
                }
                this.isReportShow = !this.isReportShow
            },
            resetTabHandle() {
                this.dealData = initDealData
            },
            /**
             * 确认上报
             * @returns {Promise.<void>}
             */
            async report() {
                var filter = this.selectedList.filter((data) => parseInt(data.isInIpSeg) == 0)
                this.selectedList = filter;
                if (this.selectedList.length <= 0) {
                    await rxPrompt.msg('请选择未上报的记录')
                    return
                }
                if (await ajaxOption({
                    res: this.api.report(this.selectedList),
                    danger: '上报失败',
                    success: '上报成功',
                })) {
                    this.isReportShow = false;
                    await this.searchPage()
                }
            },
            /**
             * 确认 ip 处置
             */
            async deal() {
                const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 0).map((obj) => obj.id)
                if (disposedIds.length !== 0) {
                    rxPrompt.msg('只能勾选未处置的数据');
                    return;
                }
                const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 0).map((obj) => obj.id)
                if (this.checkSelected() && await ajaxOption({
                    res: slogHotIpApi.deal({
                        v1: ids.map(id => this.slogHotIpMap.get(id)),
                        v2: this.dealData.action,
                        v3: this.dealData.msg,
                    }),
                    danger: '处置失败',
                    success: '处置成功',
                })) {
                    await this.searchPage()
                }
            },
            /**
             * 取消处置
             */
            async cancelDeal() {
                const disposedIds = this.selectedList.filter((obj) => parseInt(obj.dealStatus) !== 1).map((obj) => obj.id)
                if (disposedIds.length !== 0) {
                    rxPrompt.msg('只能勾选已处置的数据');
                    return;
                }
                const ids = this.selectedList.filter((obj) => parseInt(obj.dealStatus) === 1).map((obj) => obj.id)
                let data = ids.map(id => this.slogHotIpMap.get(id));
                data.forEach(item => {
                    item.block = 0;
                });
                if (this.checkSelected() && await ajaxOption({
                    res: slogHotIpApi.cancelDeal(data),
                    danger: '取消处置失败',
                    success: '取消处置成功',
                })) {
                    await this.searchPage()
                }
            },
            async showFilingWindow(type, ip) {
                const filingService = filingWindowMap.get(type)
                this.filingWindow = {
                    ...this.filingWindow,
                    title: filingService.title,
                    data: await slogHotIpDomainApi.list({
                        ip,
                        filingStatus: filingService.filingStatus,
                    }),
                    show: true,
                }
            },
            humpToLine(str) {
                return StringStyleUtil.convert(Camel, Snake, str)
            },
        },
        computed: {
            fieldFormatter() {
                return fieldFormatter
            },
        },
    })

})()